/*    */ package database;
/*    */

import com.alibaba.druid.pool.DruidPooledConnection;
import scripting.AbstractPlayerInteraction;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */
/*    */ 
/*    */ public class DatabaseScriptAPI {
/*    */   public static List<Map<String, Object>> SQLSelect(String sql, Object... data) {
/* 14 */     List<Map<String, Object>> ret = new ArrayList<>();
/* 15 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/* 16 */         PreparedStatement ps = con.prepareStatement(sql)) {
/* 17 */       for (int i = 0; i < data.length; i++) {
/* 18 */         ps.setObject(i + 1, data[i]);
/*    */       }
/* 20 */       try (ResultSet rs = ps.executeQuery()) {
/* 21 */         ResultSetMetaData metaData = ps.getMetaData();
/* 22 */         while (rs.next()) {
/* 23 */           Map<String, Object> rsdata = new HashMap<>();
/* 24 */           for (int j = 0; j < metaData.getColumnCount(); j++) {
/* 25 */             String columnLabel = metaData.getColumnLabel(j + 1);
/* 26 */             rsdata.put(columnLabel, rs.getObject(columnLabel));
/*    */           } 
/* 28 */           if (!rsdata.isEmpty()) {
/* 29 */             ret.add(rsdata);
/*    */           }
/*    */         }
/*    */       
/*    */       } 
/* 34 */     } catch (SQLException e) {
/* 35 */       AbstractPlayerInteraction.log.error("查询数据出错", e);
/*    */     } 
/* 37 */     return ret;
/*    */   }
/*    */   
/*    */   public static int SQLInsert(String sql, Object... data) {
/* 41 */     int ret = 0;
/* 42 */     DruidPooledConnection con = null;
/*    */     try {
/* 44 */       con = DatabaseConnection.getInstance().getConnection();
/* 45 */       con.setAutoCommit(false);
/* 46 */       try (PreparedStatement ps = con.prepareStatement(sql, 1)) {
/* 47 */         for (int i = 0; i < data.length; i++) {
/* 48 */           ps.setObject(i + 1, data[i]);
/*    */         }
/* 50 */         ret = ps.executeUpdate();
/*    */       } 
/* 52 */       con.commit();
/* 53 */     } catch (SQLException e) {
/*    */       try {
/* 55 */         con.rollback();
/* 56 */       } catch (SQLException sQLException) {}
/*    */       
/* 58 */       AbstractPlayerInteraction.log.error("插入数据出错", e);
/*    */     } finally {
/*    */       try {
/* 61 */         con.setAutoCommit(true);
/* 62 */         con.close();
/* 63 */       } catch (SQLException sQLException) {}
/*    */     } 
/*    */     
/* 66 */     return ret;
/*    */   }
/*    */   
/*    */   public static int SQLUpdate(String sql, Object... data) {
/* 70 */     int ret = 0;
/* 71 */     DruidPooledConnection con = null;
/*    */     try {
/* 73 */       con = DatabaseConnection.getInstance().getConnection();
/* 74 */       con.setAutoCommit(false);
/* 75 */       try (PreparedStatement ps = con.prepareStatement(sql)) {
/* 76 */         for (int i = 0; i < data.length; i++) {
/* 77 */           ps.setObject(i + 1, data[i]);
/*    */         }
/* 79 */         ret = ps.executeUpdate();
/*    */       } 
/* 81 */       con.commit();
/* 82 */     } catch (SQLException e) {
/*    */       try {
/* 84 */         con.rollback();
/* 85 */       } catch (SQLException sQLException) {}
/*    */       
/* 87 */       AbstractPlayerInteraction.log.error("更新数据出错", e);
/*    */     } finally {
/*    */       try {
/* 90 */         con.setAutoCommit(true);
/* 91 */         con.close();
/* 92 */       } catch (SQLException sQLException) {}
/*    */     } 
/*    */     
/* 95 */     return ret;
/*    */   }
/*    */ }


/* Location:              C:\PGX\Server.jar!\database\DatabaseScriptAPI.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */